package com.elvis.chat.ui;

import com.elvis.chat.service.Contact;
import com.elvis.chat.service.PresenceAdapter;
import com.elvis.chat.service.aidl.IBeemRosterListener;
import com.elvis.chat.service.aidl.IChatManager;
import com.elvis.chat.service.aidl.IRoster;
import com.elvis.chat.service.aidl.IXmppFacade;

import org.jivesoftware.smack.Connection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smackx.Form;
import org.jivesoftware.smackx.muc.MultiUserChat;

import android.app.Activity;
import android.content.ComponentName;
import android.content.ServiceConnection;
import android.content.pm.ServiceInfo;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;

import java.util.List;

// ELVIS 群聊
public class MultiChat extends Activity {
    private String TAG = MultiChat.class.getSimpleName();
    private final ServiceConnection mConn = new ElvisServiceConnection();
    private IXmppFacade mXmppFacade;
    private IRoster mRoster;
    private Contact mContact;
    private IChatManager mChatManager;
    private Handler mHandler = new Handler();

    private final BeemRosterListener mBeemRosterListener = new BeemRosterListener();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);

	System.out.println(">>>"+TAG);

    }



    private final class ElvisServiceConnection implements ServiceConnection {

	/**
	 * Constructor.
	 */
	public ElvisServiceConnection() {
	}

	/**
	 * {@inheritDoc}.
	 */
	@Override
	public void onServiceConnected(ComponentName name, IBinder service) {
	    mXmppFacade = IXmppFacade.Stub.asInterface(service);
	    try {
		mRoster = mXmppFacade.getRoster();
		if (mRoster != null)
		    mRoster.addRosterListener(mBeemRosterListener);
		mChatManager = mXmppFacade.getChatManager();
		if (mChatManager != null) {
		    //mChatManager.addChatCreationListener(mChatManagerListener);
		    //changeCurrentChat(mContact);
		}
	    } catch (RemoteException e) {
		Log.e(TAG, e.getMessage());
	    }
	}

	/**
	 * {@inheritDoc}.
	 */
	@Override
	public void onServiceDisconnected(ComponentName name) {
	    mXmppFacade = null;
	    try {
		mRoster.removeRosterListener(mBeemRosterListener);
		//mChatManager.removeChatCreationListener(mChatManagerListener);
	    } catch (RemoteException e) {
		Log.e(TAG, e.getMessage());
	    }
	}
    }

    private class BeemRosterListener extends IBeemRosterListener.Stub{

	@Override
	public void onEntriesAdded(List<String> addresses) throws RemoteException {

	}

	@Override
	public void onEntriesDeleted(List<String> addresses) throws RemoteException {

	}

	@Override
	public void onEntriesUpdated(List<String> addresses) throws RemoteException {

	}

	@Override
	public void onPresenceChanged(final PresenceAdapter presence) throws RemoteException {
	    if (mContact.getJID().equals(StringUtils.parseBareAddress(presence.getFrom()))) {
		mHandler.post(new Runnable() {
		    public void run() {
			mContact.setStatus(presence.getStatus());
			mContact.setMsgState(presence.getStatusText());
			//updateContactInformations();
			//updateContactStatusIcon();
		    }
		});
	    }
	}

    }
}
